import Vue from 'vue'
import { getSystemSettings } from '@/api/public'

import {
  SYSTEM_SETTINGS,
  SIDEBAR_TYPE
} from '@/store/mutation-types'

const app = {
  state: {
    sidebar: true,
    fixSiderbar: true,
    device: 'desktop',
    systemSettings: {
      name: '宝鼎商城后台管理',
      logo: '',
      profile: '',
      copyright: ''
    }
  },
  mutations: {
    SET_SIDEBAR_TYPE: (state, type) => {
      state.sidebar = type
      Vue.ls.set(SIDEBAR_TYPE, type)
    },
    SET_SYSTEM_SETTINGS: (state, SETTINGS) => {
      Vue.ls.set(SYSTEM_SETTINGS, SETTINGS)
      state.systemSettings = Object.assign(state.systemSettings, SETTINGS)
    },
  },
  actions: {
    setSidebar ({ commit }, type) {
      commit('SET_SIDEBAR_TYPE', type)
    },
    GetSystemSettings ({ commit }) {
      return new Promise((resolve, reject) => {
        getSystemSettings().then(response => {
          commit('SET_SYSTEM_SETTINGS', response.data)
          resolve()
        }).catch(error => {
          reject(error)
        })
      })
    },
  }
}

export default app